<template>
  <el-card class="my-exam">
    <div class="top" style="padding-right: 40px">
      <div class="left" style="float: left">
        <el-form :inline="true" :model="searchForm">
          <el-form-item label="身份">
            <el-select v-model="searchForm.state" placeholder="请选择">
              <el-option
                v-for="(item, index) in options"
                :key="index"
                :label="item"
                :value="item">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="search">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <el-table
      :data="tableData"
      stripe
      style="width: 100%">
      <el-table-column
        prop="id"
        label="id"
        width="100"
      >
      </el-table-column>
      <el-table-column
        prop="username"
        width="140"
        label="用户名称">
      </el-table-column>
      <el-table-column
        prop="email"
        label="邮箱"
        width="180">
      </el-table-column>
      <el-table-column
        prop="phone"
        label="手机号"
        width="180">
      </el-table-column>
      <el-table-column
        prop="registerTime"
        label="注册时间"
        width="180">
      </el-table-column>
      <el-table-column
        width="180"
        label="身份">
        <template slot-scope="scope">
          <el-tag
            :type="getRole(scope.row.roles) == '管理者' ? 'primary' : ''"
            size="mini"
          >{{getRole(scope.row.roles)}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="操作">
        <template slot-scope="scope">
          <el-button size="small" @click="del(scope.row.id)" type="danger">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="users.total"
      :page-size="users.pageSize"
      @current-change="getUsers"
    >
    </el-pagination>
  </el-card>
</template>

<script>
  import { mapGetters } from 'vuex'

  export default {
    data() {
      return {

        searchForm: {
          state: null,
        },
        options:["管理者", "普通用户", "all"],
        tableData:[],
        users: {},
      }
    },
    computed: {
      ...mapGetters([
        'userInfo',
      ]),
    },
    async created () {
      await this.getUsers(1);
    },
    methods: {
      search() {
        let index = 0;
        if (this.searchForm.state == 'all') {
          this.tableData = this.users.list;
          return;
        }
        this.tableData = [];
        this.users.list.filter((item) => {
          if (item.roles[0].roleZh == this.searchForm.state) {
            this.tableData[index++] = item;
          }
        })
      },
      async getUsers(page) {
        const { data: res } = await this.$http.get('admin/users/limit', {
          params: {
            pageNum:page,
          }
        });
        if (res.code == 200) {
          return
        }
        this.users = res.data.users;
        this.tableData = res.data.users.list;
      },
      async del(id) {
        const { data: res } = await this.$http.delete('admin/user', {
          params: {
            ids: id,
          }
        });
        if (res.code == 200) {
          return this.$message.error('删除失败')
        }
        this.$message.success('删除成功')
        this.getUsers(1);
      },
      getRole(element) {
        if (element[0] == undefined) {
          return "";
        }
        return element[0].roleZh;
      },
    }
  }
</script>

<style lang="scss" scoped>
  .my-exam{
    width: 1200px;
  }
</style>

